﻿using Microsoft.Extensions.DependencyInjection;

namespace Devonline.MessageQueue.RabbitMQ;

public static class ServiceExtensions
{
    /// <summary>
    /// 注册消息队列服务
    /// </summary>
    /// <param name="services">依赖注入服务</param>
    /// <param name="endpoint">消息队列设置</param>
    /// <param name="serviceLifetime">MessageQueue 生命周期</param>
    public static IServiceCollection AddKeyedMessageQueue(this IServiceCollection services, IRabbitMQEndpoint endpoint, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
    {
        services.AddSingleton<IMessageQueueEndpoint>(endpoint);
        switch (serviceLifetime)
        {
            case ServiceLifetime.Transient:
                services.AddKeyedTransient<IMessageQueueService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
            case ServiceLifetime.Scoped:
                services.AddKeyedScoped<IMessageQueueService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
            case ServiceLifetime.Singleton:
            default:
                services.AddKeyedSingleton<IMessageQueueService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
        }

        return services.AddKeyedRabbitMQService(endpoint);
    }
    /// <summary>
    /// 注册 Apache RabbitMQ 消息队列服务
    /// </summary>
    /// <param name="services">依赖注入服务</param>
    /// <param name="endpoint">消息队列设置</param>
    /// <param name="serviceLifetime">RabbitMQService 生命周期</param>
    /// <returns></returns>
    public static IServiceCollection AddKeyedRabbitMQService(this IServiceCollection services, IRabbitMQEndpoint endpoint, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
    {
        services.AddSingleton<IRabbitMQEndpoint>(endpoint);
        switch (serviceLifetime)
        {
            case ServiceLifetime.Transient:
                services.AddKeyedTransient<IRabbitMQService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
            case ServiceLifetime.Scoped:
                services.AddKeyedScoped<IRabbitMQService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
            case ServiceLifetime.Singleton:
            default:
                services.AddKeyedSingleton<IRabbitMQService, RabbitMQService>(endpoint.Name, (serviceProvider, _) => new RabbitMQService(serviceProvider.GetRequiredService<ILogger<RabbitMQService>>(), endpoint));
                break;
        }

        return services;
    }

    /// <summary>
    /// 注册消息队列服务
    /// </summary>
    /// <param name="services">依赖注入服务</param>
    /// <param name="endpoint">消息队列设置</param>
    /// <param name="serviceLifetime">MessageQueue 生命周期</param>
    public static IServiceCollection AddMessageQueue(this IServiceCollection services, IRabbitMQEndpoint endpoint, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
    {
        services.AddSingleton<IMessageQueueEndpoint>(endpoint);
        switch (serviceLifetime)
        {
            case ServiceLifetime.Transient:
                services.AddTransient<IMessageQueueService, RabbitMQService>();
                break;
            case ServiceLifetime.Scoped:
                services.AddScoped<IMessageQueueService, RabbitMQService>();
                break;
            case ServiceLifetime.Singleton:
            default:
                services.AddSingleton<IMessageQueueService, RabbitMQService>();
                break;
        }

        return services.AddRabbitMQService(endpoint, serviceLifetime);
    }
    /// <summary>
    /// 注册 Apache RabbitMQ 消息队列服务
    /// </summary>
    /// <param name="services">依赖注入服务</param>
    /// <param name="endpoint">消息队列设置</param>
    /// <param name="serviceLifetime">RabbitMQService 生命周期</param>
    /// <returns></returns>
    public static IServiceCollection AddRabbitMQService(this IServiceCollection services, IRabbitMQEndpoint endpoint, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
    {
        services.AddSingleton<IRabbitMQEndpoint>(endpoint);
        switch (serviceLifetime)
        {
            case ServiceLifetime.Transient:
                services.AddTransient<IRabbitMQService, RabbitMQService>();
                break;
            case ServiceLifetime.Scoped:
                services.AddScoped<IRabbitMQService, RabbitMQService>();
                break;
            default:
            case ServiceLifetime.Singleton:
                services.AddSingleton<IRabbitMQService, RabbitMQService>();
                break;
        }

        return services;
    }
}